<?php
/**
 * poste/load-index.phtml
 */
?>
<?php

use RHX\Helper\Number;
?>
<?php
// Variables
$postes		 = $this->postes;
$colleges	 = $this->colleges;
?>
<div class="list-container">
    <table class="table table-striped list-table">
	<thead>
	    <tr>

		<td width="25%">Code</td>
		<td width="35%">Libellé</td>
		<td width="10%">Niveau</td>
		<td width="10%">Echelon</td>
		<td width="10%">Collège</td>
		<td width="10%">&nbsp;</td>
	    </tr>
	</thead>
	<tbody>
	    <?php foreach ($postes as $p): ?>
		<?php $college = $colleges[(int) $p->college]; ?>
    	    <tr>

    		<td><a href="#" class="edit-code" data-type="text" data-pk="<?php echo $p->id; ?>" data-name="code"><?php echo $p->code; ?></a></td>
    		<td><a href="#" class="edit-libelle" data-type="text" data-pk="<?php echo $p->id; ?>" data-name="libelle"><?php echo $p->libelle; ?></a></td>
    		<td><a href="#" class="edit-niveau" data-type="select" data-pk="<?php echo $p->id; ?>" data-name="niveau">Niveau <?php echo Number::getRomanNumber((int) $p->niveau); ?></a></td>
    		<td><a href="#" class="edit-echelon" data-type="select" data-pk="<?php echo $p->id; ?>" data-name="echelon">Echelon <?php echo $p->echelon; ?></a></td>
    		<td><a href="#" class="edit-college" data-type="select" data-pk="<?php echo $p->id; ?>" data-name="college"><?php echo $college; ?></a></td>
    		<td>
    		    <a href='#' data-id="<?php echo $p->id; ?>" data-libelle="<?php echo $p->libelle; ?>" class="delete-x">
    			<img src='<?php echo $this->basePath() . '/img/rhx/delete-16x16.png' ?>' alt='Effacer' title='Effacer'>
    		    </a>
    		</td>
    	    </tr>
	    <?php endforeach; ?>
	</tbody>
    </table>
</div>

<script type="text/javascript">
// Démarrage
    $(function() {
	// Edition des libellés
	$('.edit-libelle').editable({
	    title: 'Edition du libellé',
	    url: '<?php echo $this->url('RHX/default', array('controller' => 'poste', 'action' => 'saveString')); ?>'
	});
	// Edition des descriptions
	$('.edit-code').editable({
	    title: 'Edition du code',
	    url: '<?php echo $this->url('RHX/default', array('controller' => 'poste', 'action' => 'saveString')); ?>'
	});
	// Edition du Niveau
<?php
$niveau = array();
for ($i = 1; $i < 6; $i++) {
    $niveau[$i] = "Niveau " . \RHX\Helper\Number::getRomanNumber($i);
}
$niveauSource	 = json_encode($niveau);
?>
	$('.edit-niveau').editable({
	    title: 'Niveau',
	    source: '<?php echo $niveauSource; ?>',
	    url: '<?php echo $this->url('RHX/default', array('controller' => 'poste', 'action' => 'saveInt')); ?>'
	});
	// Edition de l'echelon
<?php
$echelon	 = array();
for ($i = 1; $i < 5; $i++) {
    $echelon[$i] = "Echelon $i";
}
$echelonSource	 = json_encode($echelon);
?>
	$('.edit-echelon').editable({
	    title: 'Echelon',
	    source: '<?php echo $echelonSource; ?>',
	    url: '<?php echo $this->url('RHX/default', array('controller' => 'poste', 'action' => 'saveInt')); ?>'
	});

	// Edition du Collège
<?php
$college	 = array();
for ($i = 1; $i < 11; $i++) {
    $college[$i] = 'College ' . \RHX\Helper\Number::getRomanNumber($i);
}
$collegeSource = json_encode($college);
?>
	$('.edit-college').editable({
	    title: 'College',
	    source: '<?php echo $this->url('RHX/default', array('controller' => 'employe', 'action' => 'collegeList')); ?>',
	    url: '<?php echo $this->url('RHX/default', array('controller' => 'poste', 'action' => 'saveInt')); ?>',
	    placement: 'left'
	});
	// Effacer
	$('.delete-x').click(function(e) {
	    e.preventDefault();
	    libelle = $(this).attr('data-libelle');
	    id = $(this).attr('data-id');
	    if (!confirm("Voulez-vous effacer le poste '" + libelle + "' ?")) {
		return false;
	    }
	    ajax = $.ajax({
		type: 'post',
		url: '<?php echo $this->url('RHX/default', array('controller' => 'poste', 'action' => 'erase')) ?>',
		data: '&id=' + id
	    });
	    ajax.done(function(result) {
		if (!result.done) {
		    alert("Une erreur est survenue !");
		}
		refresh();
	    });
	});
    });
</script>
